﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ICE.Vacations.Logica.Consultas;
using ICE.Vacations.Logica.Solicitudes;
using ICE.Vacations.Logica.Users;
using ICE.Vacations.Entidades;

namespace ICE.Vacations.WebASP
{
    public partial class enviarSolicitud : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cargarSolicitudes();
            }
            catch
            {
                Response.Redirect("Default.aspx", true);
            }
        }

        public void cargarSolicitudes()
        {
            if (!filtro.Text.Trim().Equals(""))
            {
                if (listaCampos.SelectedValue.Equals("Nombre"))
                    listaSolicitudes.DataSource = RequestManager.consultarSolicitud(Request.Cookies["username"].Value, "", "", filtro.Text);
                else
                    listaSolicitudes.DataSource = RequestManager.consultarSolicitud(Request.Cookies["username"].Value, listaCampos.SelectedValue, filtro.Text);
            }
            else
            {
                listaSolicitudes.DataSource = Logica.Solicitudes.RequestManager.consultarSolicitud(Request.Cookies["username"].Value);

            }
            listaSolicitudes.DataBind();
        }

        protected void filtro_TextChanged(object sender, EventArgs e)
        {

        }

        protected void listaSolicitudes_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int indice = Int32.Parse(e.CommandArgument.ToString());
            if (e.CommandName.Equals("enviar_Solicitud"))
            {
                if (String.IsNullOrEmpty(txtCaptcha.Text))
                {
                    System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Debe digitar el código de verificación antes de enviar la solicitud','',alert.Information);", true);
                    return;
                }
                else if (!txtCaptcha.Text.Equals(UsersManager.getCapcha())) 
                {
                    System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Código de verificación inválido','',alert.Information);", true);
                    return;
                }






                //Response.Write("<script>alert('This is Alert1');</script>");
                if (subirArchivo(RequestManager.getSolicitudes()[indice].requestID))
                {
                    RequestManager.enviarSolicitud(Request.Cookies["username"].Value, RequestManager.getSolicitudes()[indice], Server.MapPath("PDF"));
                }
                else
                {
                    System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Por favor seleccione un archivo','',alert.Information);", true);
                }


            }
        }

        public bool subirArchivo(string RequestID)
        {
            //if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
            //{
            //    try
            //    {
                    
            //        System.IO.FileInfo file= new System.IO.FileInfo(System.IO.Path.GetFileName(File1.PostedFile.FileName));
            //        if (file.Extension.ToUpper().Equals("PDF"))
            //            return false;
            //        string SaveLocation = Server.MapPath("PDF") + "\\" + RequestID+".PDF";

            //        File1.PostedFile.SaveAs(SaveLocation);
                    
            //        System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('El archivo ha sido cargado.','Error',alert.Information);", true);

            //        return true;
            //    }
            //    catch (Exception ex)
            //    {
            //        System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Error al cargar el archivo');", true);
            //        return false;
            //    }
                
            //}
            //else
            //{  
            //    return false;
                
            //}
            return true;
        }

        protected void btnSolCodigo_Click(object sender, EventArgs e)
        {
            try
            {
                UsersManager.sendCapcha(Request.Cookies["username"].Value);
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Código de Verificación enviado.');", true);
            }
            catch (Exception ex)
            {
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Error al enviar el código de verificación');", true);

            }
        }

    }
}